package com.mallcai.bigdata.ladon.service.peekdata.sql;

import com.mallcai.bigdata.ladon.dpl.vo.DataType;
import com.mallcai.bigdata.ladon.dpl.vo.PeekRuleType;
import com.mallcai.bigdata.ladon.service.peekdata.RuleFactory;
import lombok.Data;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-03 14:23
 */
@Data
public class FilterField implements SqlField {

    private String fieldName;

    private PeekRuleType rule;

    private String value;

    private DataType dataType;

    public FilterField(String fieldName, String rule, String value, String dataType) {
        this.fieldName = checkNotNull(fieldName, "字段名称不能为空");
        this.rule = checkNotNull(PeekRuleType.getByCode(rule), "非法的规则类型:{}",rule);
        this.value = checkNotNull(value, "规则的值不能为空");
        this.dataType = checkNotNull(DataType.getByCode(dataType), "非法的数据类型:{}",dataType);
    }

    @Override
    public String toSql() {
        String ruleString = RuleFactory.ruleString(dataType, rule, value);
        checkNotNull(ruleString, "不支持的规则[字段名称:{}, 运算类型:{}]",fieldName, rule.getCode());
        StringBuilder ans = new StringBuilder();
        ans.append(this.fieldName).append(SqlConstants.SPACE).append(ruleString);
        return ans.toString();
    }

}
